package com.madao.im.config;

import com.madao.im.config.annotation.NoAuthorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 拦截器
 *
 * @author chengjunchao
 * @version 1.0.0
 * @date 2021/12/6 15:22
 */
@Component
@Slf4j
public class AuthorizeInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String uri = request.getRequestURI();
        //如果不是方法 则让通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        //如果有NoAuthorization注解 则不鉴权
        if (method.getAnnotation(NoAuthorization.class) != null) {
            return true;
        }
        boolean bLogin = true;
        if (!bLogin) {
            return false;
        }
        return true;
    }

}
